package com.sky.controller.admin;

import com.sky.entity.User;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.service.ReportService;
import com.sky.service.UserService;
import com.sky.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/admin/report")
@Slf4j
@Api(tags = "统计分析相关接口")
public class ReportController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private UserService userService;
    @Autowired
    private ReportService reportService;




    @ApiOperation("营业额统计")
    @GetMapping("/turnoverStatistics")
    public Result turnoverStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                     @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        log.info("营业额统计{},{}", begin, end);

        TurnoverReportVO turnoverReportVO = orderService.getTurnoverStatics(begin, end);
        return Result.success(turnoverReportVO);
    }

    @ApiOperation("用户统计接口")
    @GetMapping("/userStatistics")
    public Result userStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                 @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        log.info("用户数量统计{}，{}", begin, end);
        UserReportVO userReportVO = userService.getNumByDate(begin, end);
        return Result.success(userReportVO);

    }
    @ApiOperation("订单统计")
    @GetMapping("/ordersStatistics")
    public Result ordersStatistics(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                 @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        log.info("用户数量统计{}，{}", begin, end);
        OrderReportVO orderReportVO = orderService.getOrdersByDate(begin, end);
        return Result.success(orderReportVO);

    }

    /**
     * 查询销量top10
     */
    @GetMapping("/top10")
    @ApiOperation("向量前十统计")
    public Result top(@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                                   @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        log.info("用户数量统计{}，{}", begin, end);
        SalesTop10ReportVO orderReportVO = orderService.getTop(begin, end);
        return Result.success(orderReportVO);
    }
    /**
     * 报表导出
     * 输出io流，要用httpServletResponse
     */
    @GetMapping(value = "/export")
    public void  exslReport(HttpServletResponse response){
        log.info("统计报表");
        reportService.businessDateByMonth(response);

    }
}
